package com.yanqu.road.server.gameplayer.module.activity;

import com.yanqu.road.entity.activity.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.cookboyactivity.CookBoyActivityProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.logic.activity.relation.RelationActivityData;
import com.yanqu.road.server.logic.activity.relation.condition.BaseRelationActivityCondition;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.manger.activity.RelationActivityRankMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ChefDuelActivityPb;
import com.yanqu.road.server.pb.CookBoyActivityPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;


//全程需要触发活动条件公共类
public class RelationActivityModule extends GeneralModule {

    //activityId type CookBoyActivityData
    protected Map<Integer, Map<Integer, RelationActivityData>> activityDataMap;

    /**
     * 临时改变的任务列表
     */
    private ArrayList<RelationActivityData> changeBaseActivityDataList = new ArrayList<>();

    private AtomicInteger changeBaseActivityCount = new AtomicInteger();

    public RelationActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public boolean loadData() {
        activityDataMap = new ConcurrentHashMap<>();
        List<ActivityInfo> activityInfoList = RelationActivityMgr.getOpenActivityInfoList();
        for(ActivityInfo activityInfo : activityInfoList){
            Map<Integer, List<UserActivityConditionData>> userActivityConditionDataMap = RelationActivityMgr.getUserActivityConditionDataMap(player.getUserId(),activityInfo.getActivityId());
            if (null != userActivityConditionDataMap) {
                for (Map.Entry<Integer, List<UserActivityConditionData>> dataEntry : userActivityConditionDataMap.entrySet()) {
                    addActivity(activityInfo, dataEntry.getValue(), dataEntry.getKey(), false);
                }
            }
        }
        addNewActivity();
        return true;
    }

    protected void addActivityData(Map<Integer, Map<Integer, RelationActivityData>> dataMap, int type,int childType){
        List<ActivityInfo> activityInfoList = RelationActivityMgr.getOpenActivityInfoList(type,childType);
        if(activityInfoList.size() > 0){
            for (ActivityInfo activityInfo : activityInfoList) {
                Map<Integer, RelationActivityData> tempMap = activityDataMap.get(activityInfo.getActivityId());
                if (null != tempMap) {
                    dataMap.put(activityInfo.getActivityId(), tempMap);
                }
            }
        }
    }

    public void addNewActivity() {
        synchronized (this) {
            List<ActivityInfo> activityInfoList = RelationActivityMgr.getOpenActivityInfoList();
            for (ActivityInfo activityInfo : activityInfoList) {
                Map<Integer, List<ActivityConditionInfo>> dataMap = RelationActivityMgr.getActivityConditionInfoMap(activityInfo.getActivityId());
                if (null != dataMap) {
                    for (int type : dataMap.keySet()) {
                        addActivity(activityInfo, new ArrayList<>(), type, true);
                    }
                }
                Map<Integer, RelationActivityData> tempActivityDataMap = getActivityDataMap(activityInfo.getActivityId());
                if (null != tempActivityDataMap) {
                    for (RelationActivityData activityData : tempActivityDataMap.values()) {
                        for (BaseRelationActivityCondition baseActivityCondition : activityData.getBaseActivityConditionList()) {
                            if (null != baseActivityCondition) {
                                baseActivityCondition.initValue();
                            }
                        }
                    }
                }
            }
        }
    }

    protected Map<Integer, RelationActivityData> getActivityDataMap(int activityId){
        return activityDataMap.get(activityId);
    }

    public Map<Integer,Map<Integer, RelationActivityData>> getActivityDataMap(){
        return activityDataMap;
    }

    protected void addActivity(ActivityInfo activityInfo, List<UserActivityConditionData> conditionDataList, int type, boolean isNew) {
        synchronized (this) {
            if (null != activityInfo) {
                if (!activityDataMap.containsKey(activityInfo.getActivityId())) {
                    activityDataMap.put(activityInfo.getActivityId(), new ConcurrentHashMap<>());
                }

                Map<Integer, RelationActivityData> dataMap = activityDataMap.get(activityInfo.getActivityId());
                if (dataMap.containsKey(type)) {
                    return;
                }
                List<ActivityConditionInfo> conditionInfoList = RelationActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId(), type);
                if (null == conditionInfoList) {
                    return;
                }

                RelationActivityData activityData = new RelationActivityData(player, activityInfo, conditionDataList);

                for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                    if (activityData.isContinueAddCondition(conditionInfo.getType())) {
                        BaseRelationActivityCondition baseActivityCondition = BaseRelationActivityCondition.createActivityCondition(player, activityData, activityInfo, conditionInfo);
                        if (null != baseActivityCondition) {
                            activityData.addCondition(baseActivityCondition);
                            if (isNew) {
                                UserActivityConditionData userConditionData = ActivityMgr.initUserActivityConditionData(player, conditionInfo);
                                activityData.addUserActivityConditionData(userConditionData);
                                //Mgr中添加
                                RelationActivityMgr.addUserActivityConditionData(getUserId(), userConditionData);
                            }
                        }
                    }
                }
                dataMap.put(type, activityData);
                activityData.addToPlayer();
            }
        }
    }

    public ActivityRewardResult getActivityReward(int activityId, int conditionId) {
        ActivityRewardResult result = new ActivityRewardResult();
        ActivityInfo openActivityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if(null == openActivityInfo){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
            return result;
        }
        UserActivityConditionData userActivityConditionData = RelationActivityMgr.getUserActivityCondition(player.getUserId(),activityId,conditionId);
        // 判断该任务是否完成
        if (null == userActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        ActivityConditionInfo activityConditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId,conditionId);
        if (null == activityConditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        String param = activityConditionInfo.getParamStr();
        List<Integer> paramList = StringUtils.stringToIntegerList(param,"\\|");
        if (userActivityConditionData.getValue().compareTo(BigInteger.valueOf(paramList.get(3))) < 0) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        Map<Integer, RelationActivityData> dataMap = activityDataMap.get(openActivityInfo.getActivityId());
        if(null == dataMap){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
            return result;
        }
        //奖励到手
        Property reward = activityConditionInfo.getReward();
        userActivityConditionData.setGetReward(true);
        eLogMoneyType crossCookBoyActivityReward = eLogMoneyType.CrossCookBoyActivityReward;
        if(openActivityInfo.getType() == eActivityType.ChefDuel.getValue()){
            crossCookBoyActivityReward = eLogMoneyType.CrossChefDuelActivityReward;
        }
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, crossCookBoyActivityReward);
        result.setReward(PropertyHelper.parsePropertyToString(reward));
        RelationActivityData activityData = dataMap.get(activityConditionInfo.getType());
        onActivityDataChange(activityData);

        LogMgr.addLogActivityReward(player.getUserId(), activityId, openActivityInfo.getType(), openActivityInfo.getChildType(),
                activityConditionInfo.getConditionId(), 0, false, result.getReward());
        return result;
    }

    public ActivityRewardResult getActivityRewardOneKey(int activityId, int conditionId) {
        ActivityRewardResult result = new ActivityRewardResult();
        ActivityInfo openActivityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if(null == openActivityInfo){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
            return result;
        }

        ActivityConditionInfo activityConditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if (null == activityConditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }

        Map<Integer, RelationActivityData> dataMap = activityDataMap.get(activityId);

        if(dataMap == null){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
            return result;
        }

        RelationActivityData activityData = dataMap.get(activityConditionInfo.getType());

        Property totalReward = new Property();

        //遍历可完成的档位
        for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
            ActivityConditionInfo conditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionData.getConditionId());
            if (conditionInfo != null) {
                UserActivityConditionData userActivityConditionData = RelationActivityMgr.getUserActivityCondition(player.getUserId(),activityId, conditionInfo.getConditionId());
                if(userActivityConditionData != null){

                    String param = conditionInfo.getParamStr();
                    List<Integer> paramList = StringUtils.stringToIntegerList(param,"\\|");
                    if (userActivityConditionData.getValue().compareTo(BigInteger.valueOf(paramList.get(3))) < 0) {
                        continue;
                    }
                    if (userActivityConditionData.isGetReward()) {
                        continue;
                    }

                    userActivityConditionData.setGetReward(true);
                    //奖励到手
                    totalReward.addProperty(PropertyHelper.parseStringToProperty(conditionInfo.getRewardStr()));

                    LogMgr.addLogActivityReward(player.getUserId(), activityId, openActivityInfo.getType(), openActivityInfo.getChildType(),
                            conditionInfo.getConditionId(), 0, false, conditionInfo.getRewardStr());
                }
            }

        }

        if(!totalReward.isNothing()){

            eLogMoneyType crossCookBoyActivityReward = eLogMoneyType.CrossCookBoyActivityReward;
            if(openActivityInfo.getType() == eActivityType.ChefDuel.getValue()){
                crossCookBoyActivityReward = eLogMoneyType.CrossChefDuelActivityReward;
            }
            player.getModule(CurrencyModule.class).addCurrency(totalReward, eLogMoneyType.Activity, crossCookBoyActivityReward);

            onActivityDataChange(activityData);


            result.setReward(PropertyHelper.parsePropertyToString(totalReward));
        }

        return result;
    }

    public ActivityRewardResult getRankReward(int activityId, int type) {
        ActivityRewardResult result = new ActivityRewardResult();
        //
        ActivityInfo openActivityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if(null == openActivityInfo){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
            return result;
        }
        Map<Integer, RelationActivityData> dataMap = activityDataMap.get(openActivityInfo.getActivityId());
        if(null == dataMap){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
            return result;
        }
        RelationActivityData activityData = dataMap.get(type);
        if (null == activityData) {
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME);
            return result;
        }
        if(!ActivityMgr.activityInRewardPeriod(openActivityInfo)){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_IN_REWARD_TIME);
            return result;
        }
        if(!ActivityMgr.activityInCanRewardPeriod(openActivityInfo)){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_REWARD_SETTLEMENT_TIME);
            return result;
        }
        UserActivityConditionData conditionData = activityData.getFirstConditionData();
        if(null == conditionData){
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_JOIN);
            return result;
        }
        if (conditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_HAS_GET_REWARD);
            return result;
        }
        int myRank;
        boolean isMaster = false;
        UnionActivityConditionData unionActivityConditionData = null;
        if(RelationActivityMgr.getRankEvent(activityId,RelationActivityMgr.EVENT_RANK_USER_TYPE) == type) {
            myRank = RelationActivityRankMgr.getActivityRank(openActivityInfo.getActivityId(), player.getUserId());
        }else {
            unionActivityConditionData = RelationActivityRankMgr.getUnionActivityConditionData(activityId, player.getUserId());
            if(null == unionActivityConditionData){
                result.setRet(GameErrorCode.E_RELATION_ACTIVITY_NO_JOIN);
                return result;
            }
            myRank = RelationActivityRankMgr.getActivityUnionRank(openActivityInfo.getActivityId(), unionActivityConditionData.getUnionUid());
            isMaster = (unionActivityConditionData.getUnionBaseInfo().getMasterUserId() == player.getUserId());
        }
        ActivityConditionInfo conditionInfo = RelationActivityMgr.getActivityConditionInfoByRank(activityId, type, myRank);
        if (conditionInfo != null) {// 添加奖励
            if(isMaster && RelationActivityMgr.getRankEvent(activityId,RelationActivityMgr.EVENT_RANK_UNION_TYPE) == type){
                player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getMasterReward(), eLogMoneyType.Activity, eLogMoneyType.CrossCookBoyActivityUnionRankReward);
                result.setReward(conditionInfo.getMasterRewardStr());
            }else {
                if(RelationActivityMgr.getRankEvent(activityId,RelationActivityMgr.EVENT_RANK_UNION_TYPE) == type) {
                    result.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(unionActivityConditionData,conditionInfo,player.getUserId()));
                    player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossCookBoyActivityUnionRankReward);
                }else {
                    player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossCookBoyActivityRankReward);
                    result.setReward(conditionInfo.getRewardStr());
                }
            }
        }else{
            result.setRet(GameErrorCode.E_RELATION_ACTIVITY_GET_REWARD_TYPE_ERROR);
            return result;
        }
        conditionData.setGetReward(true);
        onActivityDataChange(activityData);
        activityData.finish(conditionData.getConditionId());


        LogMgr.addLogActivityReward(player.getUserId(), activityId, openActivityInfo.getType(), openActivityInfo.getChildType(),
                conditionInfo.getConditionId(), myRank, false, result.getReward());
        return result;
    }


    protected int checkGetReward(int activityId,int conditionType){
        ActivityInfo openActivityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        if(null == openActivityInfo){
            return GameErrorCode.E_ACTIVITY_OVER_TIME;
        }
        Map<Integer, RelationActivityData> dataMap = activityDataMap.get(openActivityInfo.getActivityId());
        if(null == dataMap){
            return GameErrorCode.E_ACTIVITY_OVER_TIME;
        }
        RelationActivityData activityData = dataMap.get(conditionType);
        if (null == activityData) {
            return GameErrorCode.E_ACTIVITY_OVER_TIME;
        }
        if(!ActivityMgr.activityInRewardPeriod(openActivityInfo)){
            return GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME;
        }
        if(!ActivityMgr.activityInCanRewardPeriod(openActivityInfo)){
            return GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME;
        }
        UserActivityConditionData conditionData = activityData.getFirstConditionData();
        if(null == conditionData){
            return GameErrorCode.E_ACTIVITY_NO_JOIN;
        }
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }
        return 0;
    }

    public ActivityRewardResult sendCrossSpecialReward(int activityId,int conditionId, int conditionType, String params, boolean oneKey) {
        ActivityRewardResult result = new ActivityRewardResult();
        ActivityConditionInfo activityConditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        Map<Integer, RelationActivityData> dataMap = activityDataMap.get(activityId);
        RelationActivityData activityData = dataMap.get(activityConditionInfo.getType());
        UserActivityConditionData conditionData = activityData.getUserActivityConditionData(conditionId);
        List<Long> userIdList = StringUtils.stringToLongList(conditionData.getParam(),"\\|");
        long homeUserId = Long.valueOf(params);
        if(userIdList.contains(homeUserId)){
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        //发送请求到跨服获取排名
        CookBoyActivityProto.CrossActivityGetSpecialRewardReqMsg.Builder reqMsg = CookBoyActivityProto.CrossActivityGetSpecialRewardReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setConditionId(conditionData.getConditionId());
        reqMsg.setParams(params);
        reqMsg.setOneKey(oneKey);
        YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_RELATION_ACTIVITY_GET_SPECIAL_REWARD, reqMsg);
        GamePlayerMgr.sendPacket(player.getUserId(), pkg);
        return result;
    }

    public ActivityRewardResult sendCrossRankReward(int activityId,int conditionId, int conditionType) {
        ActivityRewardResult result = new ActivityRewardResult();
        int ret = checkGetReward(activityId,conditionType);
        if(ret != 0){
            result.setRet(ret);
            return result;
        }
        ActivityConditionInfo activityConditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        Map<Integer, RelationActivityData> dataMap = activityDataMap.get(activityId);
        RelationActivityData activityData = dataMap.get(activityConditionInfo.getType());
        UserActivityConditionData conditionData = activityData.getFirstConditionData();
        String unionUid = "";
        //新加的商会活动就加一加
       /* if(activityConditionInfo.getType() == eGamePlayerEventType.CookBoy1IntegralUnionCrossRank.getValue()) {
            UnionActivityConditionData unionActivityConditionData = RelationActivityRankMgr.getUnionActivityConditionData(activityId, player.getUserId());
            if (null == unionActivityConditionData) {
                result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
                return result;
            }
            unionUid = unionActivityConditionData.getUnionUid();
        }*/
        //发送请求到跨服获取排名
        CrossActivityProto.CrossActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setConditionId(conditionData.getConditionId());
        reqMsg.setUnionUid(unionUid);
        YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_RELATION_ACTIVITY_GET_RANK_REWARD, reqMsg);
        GamePlayerMgr.sendPacket(player.getUserId(), pkg);
        return result;
    }

    public ActivityRewardResult getCrossRankReward(int activityId, int conditionId, int myRank) {
        ActivityRewardResult result = new ActivityRewardResult();
        ActivityInfo activityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        ActivityConditionInfo activityConditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        Map<Integer, RelationActivityData> dataMap = activityDataMap.get(activityId);
        RelationActivityData activityData = dataMap.get(activityConditionInfo.getType());
        UserActivityConditionData conditionData = activityData.getFirstConditionData();
        ActivityConditionInfo conditionInfo = RelationActivityMgr.getActivityConditionInfoByRank(activityId, activityConditionInfo.getType(), myRank);
        if(null == conditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        if(null == conditionData || conditionData.isGetReward()){
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        if(RelationActivityMgr.getRankEvent(activityId,RelationActivityMgr.EVENT_RANK_UNION_TYPE) == conditionInfo.getType()) {
            UnionActivityConditionData unionActivityConditionData = RelationActivityRankMgr.getUnionActivityConditionData(activityId, player.getUserId());
            if(player.getUserId() == unionActivityConditionData.getUnionBaseInfo().getMasterUserId()){
                player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getMasterReward(), eLogMoneyType.Activity, eLogMoneyType.CrossCookBoyActivityUnionRankReward);
                result.setReward(conditionInfo.getMasterRewardStr());
            }else {
                //根据职位获取奖励
                result.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(unionActivityConditionData,conditionInfo,player.getUserId()));
                player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossCookBoyActivityUnionRankReward);

            }
        }else {
            player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossCookBoyActivityRankReward);
            result.setReward(conditionInfo.getRewardStr());
        }
        conditionData.setGetReward(true);
        onActivityDataChange(activityData);
        activityData.finish(conditionData.getConditionId());

        String rewardStr = result.getReward();
        if (rewardStr != null) {
            Property property = PropertyHelper.parseStringToProperty(rewardStr);
            if (property != null) {
                //记录跨服称号包含的区服id
                property.getGoods().forEach((k,v)->{
                    if (GoodsMgr.isDecorationGoods(k)) {
                        player.getModule(DecorationModule.class).setServerIds(k, activityInfo.getActivityId(), activityInfo.getServerListStr());
                    }
                });
                player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr,activityInfo.getServerListStr());
            }
        }

        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                activityConditionInfo.getConditionId(), myRank, false, result.getReward());
        return result;
    }

    public ActivityRewardResult getCrossSpecialReward(int activityId, int conditionId, String params, String crossParams) {
        ActivityRewardResult result = new ActivityRewardResult();
        ActivityInfo activityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        ActivityConditionInfo activityConditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        Map<Integer, RelationActivityData> dataMap = activityDataMap.get(activityId);
        RelationActivityData activityData = dataMap.get(activityConditionInfo.getType());
        UserActivityConditionData conditionData = activityData.getUserActivityConditionData(conditionId);
        if(activityConditionInfo.getType() == eGamePlayerEventType.CookBoy3OpenBanquetTimes.getValue() ||
                activityConditionInfo.getType() == eGamePlayerEventType.CookBoy3AllBanquetUserCount.getValue() ||
                activityConditionInfo.getType() == eGamePlayerEventType.ChefDuel3AllBanquetUserCount.getValue()){
            long homeUserId = Long.valueOf(params);
            List<Long> userIdList = StringUtils.stringToLongList(conditionData.getParam(),"\\|");
            BigInteger someData = new BigInteger(crossParams);
            if(activityConditionInfo.getParamList().get(3).compareTo(someData) > 0){
                result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
                return result;
            }
            if(userIdList.contains(homeUserId)){
                result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
                return result;
            }
            eLogMoneyType son = eLogMoneyType.CrossCookBoy3ActivitySpecialReward;
            if(activityConditionInfo.getType() == eActivityType.ChefDuel.getValue()){
                son = eLogMoneyType.CrossChefDuel3ActivitySpecialReward;
            }
            player.getModule(CurrencyModule.class).addCurrency(activityConditionInfo.getReward(), eLogMoneyType.Activity, son);
            result.setReward(activityConditionInfo.getRewardStr());
            userIdList.add(homeUserId);
            conditionData.setParam(StringUtils.listToString(userIdList,"|"));
            onActivityDataChange(activityData);
        }else{
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }

        String rewardStr = result.getReward();
        if (rewardStr != null) {
            Property property = PropertyHelper.parseStringToProperty(rewardStr);
            if (property != null) {
                //记录跨服称号包含的区服id
                property.getGoods().forEach((k,v)->{
                    if (GoodsMgr.isDecorationGoods(k)) {
                        player.getModule(DecorationModule.class).setServerIds(k, activityInfo.getActivityId(), activityInfo.getServerListStr());
                    }
                });
                player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr,activityInfo.getServerListStr());
            }
        }

        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                activityConditionInfo.getConditionId(), 0, false, result.getReward());
        return result;
    }

    public ActivityRewardResult getCrossSpecialRewardOneKey(int activityId, int conditionId, String params, String crossParams) {
        ActivityRewardResult result = new ActivityRewardResult();

        ActivityInfo activityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);

        //活动不存在
        if(activityInfo == null) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }

        //点击的领奖档位
        ActivityConditionInfo activityConditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(activityConditionInfo == null) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }

        if(activityConditionInfo.getType() == eGamePlayerEventType.CookBoy3OpenBanquetTimes.getValue() ||
                activityConditionInfo.getType() == eGamePlayerEventType.CookBoy3AllBanquetUserCount.getValue()||
                activityConditionInfo.getType() == eGamePlayerEventType.ChefDuel3AllBanquetUserCount.getValue()) {

            Map<Integer, RelationActivityData> dataMap = activityDataMap.get(activityId);

            RelationActivityData activityData = dataMap.get(activityConditionInfo.getType());

            //完成值
            BigInteger someData = new BigInteger(crossParams);

            //领取目标玩家的档位奖励
            long homeUserId = Long.valueOf(params);

            Property totalReward = new Property();

            //遍历可完成的档位
            for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
                ActivityConditionInfo conditionInfo =  RelationActivityMgr.getActivityConditionInfo(activityId, conditionData.getConditionId());
                if(conditionInfo != null) {
                    List<Long> userIdList = StringUtils.stringToLongList(conditionData.getParam(),"\\|");
                    if(conditionInfo.getParamList().get(3).compareTo(someData) > 0) {
                        continue;
                    }

                    //领取过该玩家的奖励
                    if(userIdList.contains(homeUserId)) {
                        continue;
                    }

                    userIdList.add(homeUserId);
                    conditionData.setParam(StringUtils.listToString(userIdList,"|"));

                    totalReward.addProperty(PropertyHelper.parseStringToProperty(conditionInfo.getRewardStr()));


                    LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                            conditionInfo.getConditionId(), 0, false, conditionInfo.getRewardStr());
                }
            }
            eLogMoneyType logMoneyType = eLogMoneyType.CrossCookBoy3ActivitySpecialReward;
            if(activityInfo.getType() == eActivityType.ChefDuel.getValue()){
                logMoneyType = eLogMoneyType.CrossChefDuel3ActivitySpecialReward;
            }

            if(!totalReward.isNothing()){

                player.getModule(CurrencyModule.class).addCurrency(PropertyHelper.parsePropertyToString(totalReward), eLogMoneyType.Activity, logMoneyType);

                totalReward.getGoods().forEach((k,v)->{
                    if (GoodsMgr.isDecorationGoods(k)) {
                        player.getModule(DecorationModule.class).setServerIds(k, activityInfo.getActivityId(), activityInfo.getServerListStr());
                    }
                });
                player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(PropertyHelper.parsePropertyToString(totalReward),activityInfo.getServerListStr());

                onActivityDataChange(activityData);

                result.setReward(PropertyHelper.parsePropertyToString(totalReward));
            }

        }
        return result;
    }

    //同步到活动中的condition改名
    public void nickNameChange(){
        List<ActivityInfo> activityInfoList = RelationActivityMgr.getOpenActivityInfoList();
        if(activityInfoList.size() > 0) {
            for(ActivityInfo activityInfo : activityInfoList){
                Map<Integer, RelationActivityData> dataMap = activityDataMap.get(activityInfo.getActivityId());
                if(null != dataMap) {
                    //所有商会的condition改掉昵称
                    /*RelationActivityData activityData = dataMap.get(CookBoyActivityMgr.CONDITION_NO1_UNION_INTEGRAL);
                    if(null != activityData){
                        List<UserActivityConditionData> dataList = activityData.getUserActivityConditionDataList();
                        if (null != dataList) {
                            for (UserActivityConditionData data : dataList) {
                                data.setNickName(player.getUserInfo().getNickName());
                            }
                        }
                    }*/
                }
            }

        }
    }

    public void onActivityDataChange(RelationActivityData activityData){
        ActivityInfo openActivityInfo = RelationActivityMgr.getOpenActivityInfo(activityData.getActivityId());
        if(openActivityInfo != null && openActivityInfo.getType() == eActivityType.ChefDuel.getValue()){
            CommonActivityProto.CommonActivityDataMsg.Builder dataMsg = ChefDuelActivityPb.parseCommonActivityDataMsg(activityData);
            if(null != dataMsg) {
                player.sendPacket(Protocol.U_CHEF_DUEL_ACTIVITY_DATA_SYNC, dataMsg);
            }
        }else {
            CommonActivityProto.CommonActivityDataMsg.Builder dataMsg = CookBoyActivityPb.parseCommonActivityDataMsg(activityData);
            if(null != dataMsg) {
                player.sendPacket(Protocol.U_COOK_BOY_ACTIVITY_DATA_SYNC, dataMsg);
            }
        }
    }

    public void syncActivity(List<RelationActivityData> activityDataList){

        int type = eActivityType.CookingBoyActivity.getValue();
        if(!activityDataList.isEmpty()){
            RelationActivityData activityData = activityDataList.get(0);
            ActivityInfo openActivityInfo = RelationActivityMgr.getOpenActivityInfo(activityData.getActivityId());
            if(openActivityInfo != null){
                type = openActivityInfo.getType();
            }
        }
        if(type == eActivityType.ChefDuel.getValue()){
            CommonActivityProto.CommonActivityDataMsg.Builder dataMsg = ChefDuelActivityPb.parseCommonActivityDataListMsg(activityDataList);
            if(null != dataMsg) {
                player.sendPacket(Protocol.U_CHEF_DUEL_ACTIVITY_DATA_SYNC, dataMsg);
            }
        }else {
            CommonActivityProto.CommonActivityDataMsg.Builder dataMsg = CookBoyActivityPb.parseCommonActivityDataListMsg(activityDataList);
            if(null != dataMsg) {
                player.sendPacket(Protocol.U_COOK_BOY_ACTIVITY_DATA_SYNC, dataMsg);
            }
        }
    }

    /**
     * 单条执行：添加到临时列表；并通知更新客户端数据
     */
    public final void onActivityDataChanged(RelationActivityData activityData) {
        if (null == activityData) {
            return;
        }
        if (!changeBaseActivityDataList.contains(activityData)) {
            synchronized (changeBaseActivityDataList) {
                changeBaseActivityDataList.add(activityData);
            }
        }

        if (changeBaseActivityCount.intValue() <= 0 && changeBaseActivityDataList.size() > 0 || changeBaseActivityDataList.size() > 20) {
            updateChangedActivityData();
        }
    }

    /**
     * 多条执行：开始时将临时列表记数器加1
     */
    public void beginChanges() {
        changeBaseActivityCount.getAndIncrement();
    }

    /**
     * 多条执行：结束时将临时任务列表减1，则通知更新客户端
     */
    public void commitChanges() {
        // 控制变量减一
        synchronized (changeBaseActivityCount) {
            int changes = changeBaseActivityCount.decrementAndGet();
            if (changes < 0) {
                log.error("Inventory changes counter is bellow zero (forgot to use BeginChanges?)!\n\n" + new Error());
                changeBaseActivityCount.set(0);
            }
            if (changes <= 0 && changeBaseActivityDataList.size() > 0) {
                updateChangedActivityData();
            }
        }
    }

    /**
     * 通知更新客户端数据，并将临时任务列表清空
     */
    private void updateChangedActivityData() {
        if (changeBaseActivityDataList.size() > 0) {
            List<RelationActivityData> changedList;
            synchronized (changeBaseActivityDataList) {
                changedList = new ArrayList<>(changeBaseActivityDataList);
                changeBaseActivityDataList.clear();
            }
            syncActivity(changedList);
        }
    }
}
